

M = require("ming_node")
M.log_display_time=false;
M.Db_table_prefix="mi_";
M.SPLIT="______";
newDatabase = {
    "host": "127.0.0.1",
    "user": "root",
    "password": "123456",
    "port": "3306",
    "database": "miapinew"
}
oldDatabase = {
    "host": "127.0.0.1",
    "user": "root",
    "password": "123456",
    "port": "3306",
    "database": "miapiold"
}
const SQL_TYPE={
    TABLE_LIST:0,
    FIELD_LIST:1,
    INDEX:2
}


M.getSql=function(dataBase,type){
    if(SQL_TYPE.TABLE_LIST==type){
       let sql=`select distinct table_name from information_schema.columns where table_schema ='${dataBase.databaseConfig.database}' and table_name  LIKE '${M.Db_table_prefix}%' ORDER BY table_name; `
       return sql;
    }
    if(SQL_TYPE.FIELD_LIST==type){
        let sql=`select CONCAT(table_name,'${M.SPLIT}',COLUMN_NAME,'${M.SPLIT}',COLUMN_TYPE,'${M.SPLIT}',COLUMN_COMMENT) full_field from information_schema.columns where table_schema ='${dataBase.databaseConfig.database}' and table_name  LIKE '${M.Db_table_prefix}%' ORDER BY table_name;` 
        console.log(sql)
        return sql;
    }
    if(SQL_TYPE.INDEX==type){
        let sql=`select CONCAT(  TABLE_NAME,'${M.SPLIT}' ,INDEX_NAME,'${M.SPLIT}', GROUP_CONCAT(COLUMN_NAME)) full_field from information_schema.statistics where table_schema='${dataBase.databaseConfig.database}' and table_name  LIKE '${M.Db_table_prefix}%' and INDEX_NAME !='PRIMARY' GROUP BY TABLE_NAME, INDEX_NAME;` 
        return sql;
    }

}

newDb = M.getMySql(newDatabase);
newDb.databaseConfig=newDatabase
M.mysql=null;
oldDb = M.getMySql(oldDatabase);
oldDb.databaseConfig=oldDatabase;


async function table_oy_nn_list_table_on_ny_list(){
    oldList=await oldDb.doSql(M.getSql(oldDb,SQL_TYPE.TABLE_LIST))
    oldList= oldList.map(u=>u.TABLE_NAME)
    newList=await newDb.doSql(M.getSql(newDb,SQL_TYPE.TABLE_LIST))
    newList= newList.map(u=>u.TABLE_NAME)
    oy_nn_list=[];
    on_ny_list=[]
    console.log("oldTableList.length",oldList.length)
    for(let i=0;i<oldList.length;i++){
        if(newList.indexOf(oldList[i])===-1){
            oy_nn_list.push(oldList[i])
            M.log(`# 删除表 ${oldList[i]}`)
        }
    }
    console.log("newTableList.length",newList.length)
    for(let i=0;i<newList.length;i++){
        if(oldList.indexOf(newList[i])===-1){
            on_ny_list.push(newList[i])
            M.log(`# 新增表 ${newList[i]}`)
        }
    }
    //新增的表
    M.table_on_ny_list=on_ny_list;
    //删除的表
    M.table_oy_nn_list=oy_nn_list;
    console.log("table_oy_nn_list",oy_nn_list)
    console.log("table_on_ny_list",on_ny_list)
}



async function field_oy_nn_list_field_on_ny_list(){
    oldList=await oldDb.doSql(M.getSql(oldDb,SQL_TYPE.FIELD_LIST))
    oldList= oldList.map(u=>u.full_field.replace(/(bigint|tinyint|smallint)\(\d+\)/g,"$1").trim())
    newList=await newDb.doSql(M.getSql(newDb,SQL_TYPE.FIELD_LIST))
    newList= newList.map(u=>u.full_field.replace(/(bigint|tinyint|smallint)\(\d+\)/g,"$1").trim())
    oy_nn_list=[];
    on_ny_list=[]
    console.log("oldTableList.length",oldList.length)
    for(let i=0;i<oldList.length;i++){
        if(newList.indexOf(oldList[i])===-1){
            //排除删除的表
            if(M.table_oy_nn_list.indexOf(oldList[i].split(M.SPLIT)[0])==-1){
                oy_nn_list.push(oldList[i])
                M.log(`# 删除字段 ${oldList[i]}`)
            }
        }
    }
    console.log("newTableList.length",newList.length)
    for(let i=0;i<newList.length;i++){
        if(oldList.indexOf(newList[i])===-1){
            //排除新增的表
            if(M.table_on_ny_list.indexOf(newList[i].split(M.SPLIT)[0])==-1){
                on_ny_list.push(newList[i])
                M.log(`# 新增字段 ${newList[i]}`)
            }
        }
    }
    console.log("field_oy_nn_list",oy_nn_list)
    console.log("field_on_ny_list",on_ny_list)
}



async function index_oy_nn_list_field_on_ny_list(){
    oldList=await oldDb.doSql(M.getSql(oldDb,SQL_TYPE.INDEX))
    oldList= oldList.map(u=>u.full_field)
    newList=await newDb.doSql(M.getSql(newDb,SQL_TYPE.INDEX))
    newList= newList.map(u=>u.full_field)
    oy_nn_list=[];
    on_ny_list=[]
    console.log("oldIndexList.length",oldList.length)
    for(let i=0;i<oldList.length;i++){
        if(newList.indexOf(oldList[i])===-1){
            oy_nn_list.push(oldList[i])
            M.log(`# 删除索引 ${oldList[i]}`)
        }
    }
    console.log("newIndexList.length",newList.length)
    for(let i=0;i<newList.length;i++){
        if(oldList.indexOf(newList[i])===-1){
            on_ny_list.push(newList[i])
            M.log(`# 新增索引 ${newList[i]}`)
        }
    }
    console.log("index_oy_nn_list",oy_nn_list)
    console.log("index_on_ny_list",on_ny_list)
}


async function main(){
    await table_oy_nn_list_table_on_ny_list();
    await field_oy_nn_list_field_on_ny_list();
    await index_oy_nn_list_field_on_ny_list();
 }

main()

setTimeout(()=>process.exit(),1000)